#include "struct.h"
#include "Fonctions.h"
#include "Menu.h"
void Quitter(void)
{
    // free loaded bitmap
    SDL_FreeSurface(Bouton1);
    SDL_FreeSurface(Image_fond);
    SDL_FreeSurface(TextSurface);
    SDL_FreeSurface(TextBouton1);
    SDL_FreeSurface(TextBouton2);
    SDL_FreeSurface(TextBouton3);
    SDL_FreeSurface(TextBouton4);

    TTF_CloseFont(Police_Titre);
    TTF_CloseFont(Police_Bouton);

    TTF_Quit();

    // all is well ;)
    printf("Exited cleanly\n");
    exit(0);
}

void MajScreen (void)
{
    // clear screen
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    // draw bitmap
    SDL_BlitSurface(Image_fond, 0, screen, &dstrect);
    SDL_BlitSurface(Bouton1, 0, screen, &rect1);
    SDL_BlitSurface(Bouton2, 0, screen, &rect2);
    SDL_BlitSurface(Bouton3, 0, screen, &rect3);
    SDL_BlitSurface(Bouton4, 0, screen, &rect4);
    /* Affiche toute la surface en 0,0 */
    SDL_BlitSurface(TextSurface, NULL, screen, &Titre);
    SDL_BlitSurface(TextBouton1, NULL, screen, &TextNew_Game);
    SDL_BlitSurface(TextBouton2, NULL, screen, &TextConfig);
    SDL_BlitSurface(TextBouton3, NULL, screen, &TextChain_Backward);
    SDL_BlitSurface(TextBouton4, NULL, screen, &TextQuitter);

    // finally, update the screen :)
    SDL_Flip(screen);
}

void New_Game(void)
{

    char Nom_Carte[50][3];
    char Temp[50];
    int idx;
    Uint32 colorkey ;
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_Flip(screen);

    SDL_Surface* bmp = SDL_LoadBMP("tapis-de-cartes2.bmp");
    if (!bmp)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return ;
    }

    SDL_Surface* Surf_Jeton = SDL_LoadBMP("Jeton.bmp");
    if (!Surf_Jeton)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return ;
    }

    Surf_Jeton = SDL_DisplayFormat( Surf_Jeton );
    colorkey = SDL_MapRGB( Surf_Jeton->format, 255, 255, 255 );

            //on met tout les pixel de couleur R 0, G 0xFF, B 0xFF transparent
    SDL_SetColorKey( Surf_Jeton, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );

    SDL_Surface* bmp1 = SDL_LoadBMP("dos-bleu.bmp");
    if (!bmp1)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return ;
    }

    SDL_Surface* bmp_Tab[3];
    memset(bmp_Tab,0,sizeof(bmp_Tab));

    SDL_Surface* tape1 = SDL_LoadBMP("push-button.bmp");// Correspond a TAPER et POSER
    if (!tape1)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return ;
    }


    // centre the bitmap on screen
    tape1 = SDL_DisplayFormat( tape1 );
    colorkey = SDL_MapRGB( tape1->format, 255, 255, 255 );

            //on met tout les pixel de couleur R 0, G 0xFF, B 0xFF transparent
    SDL_SetColorKey( tape1, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );

    for(idx = 0;idx<4;idx++)
    {
        doscarte1[idx].x = 60-(2*idx);
        doscarte1[idx].y = 60-(2*idx);

    }

    for(idx = 0;idx<4;idx++)
    {
        doscarte2[idx].x = 650-(2*idx);
        doscarte2[idx].y = 60-(2*idx);
    }

    for(idx = 0;idx<4;idx++)
    {
        doscarte3[idx].x = 650-(2*idx);
        doscarte3[idx].y = 360-(2*idx);
    }

    for(idx = 0;idx<4;idx++)
    {
        doscarte4[idx].x = 60-(2*idx);
        doscarte4[idx].y = 360-(2*idx);

    }

    Rect_Quitter.x=2;
    Rect_Quitter.y=2;

    Rect_TextQuitter.x= (Surf_BoutonQuitter->w/2) - (Surf_TextQuitter->w/2) +2;
    Rect_TextQuitter.y= (Surf_BoutonQuitter->h/2) - (Surf_TextQuitter->h/2) +2;

    RectJeton[0].x =doscarte1[0].x + (bmp1->w /2) - (Surf_Jeton->w/2);
    RectJeton[0].y =doscarte1[0].y + (bmp1->h /2) - (Surf_Jeton->h/2);

    RectJeton[1].x =doscarte2[0].x + (bmp1->w /2) - (Surf_Jeton->w/2);
    RectJeton[1].y =doscarte2[0].y + (bmp1->h /2) - (Surf_Jeton->h/2);

    RectJeton[2].x =doscarte3[0].x + (bmp1->w /2) - (Surf_Jeton->w/2);
    RectJeton[2].y =doscarte3[0].y + (bmp1->h /2) - (Surf_Jeton->h/2);

    RectJeton[3].x =doscarte4[0].x + (bmp1->w /2) - (Surf_Jeton->w/2);
    RectJeton[3].y =doscarte4[0].y + (bmp1->h /2) - (Surf_Jeton->h/2);

    Carte2.x = (((screen->w)/2) - ((bmp1->w)/2));
    Carte2.y = (((screen->h)/2) - ((bmp1->h)/2));

    Carte1.x = Carte2.x - ((bmp1->w)/2) ;
    Carte1.y = Carte2.y;

    Carte3.x = Carte2.x + ((bmp1->w)/2);
    Carte3.y = Carte2.y;

    Push1.x = (((screen->w)/2) - ((tape1->w)/2));
    Push1.y = doscarte1[0].y + ((tape1->h)*2/3) ;

    Push2.x = (((screen->w)/2) - ((tape1->w)/2));
    Push2.y = doscarte4[0].y + ((tape1->h)*2/3) ;

    RectTaper.x = (((screen->w)/2) - ((TextTaper->w) /2));
    RectTaper.y = doscarte1[0].y+ ((bmp1->h)*1/3);
    RectTaper.w = TextTaper->w;
    RectTaper.h = TextTaper->h;

    RectPoser.x = (((screen->w)/2) - ((TextPoser->w) /2));
    RectPoser.y = doscarte4[0].y + ((bmp1->h)*1/3) ;
    RectPoser.w = TextPoser->w;
    RectPoser.h = TextPoser->h;

    Rect_NomJoueur[0].x = doscarte1[2].x;
    Rect_NomJoueur[0].y = doscarte1[0].y + bmp1->h;

    Rect_NomJoueur[1].x = doscarte2[2].x;
    Rect_NomJoueur[1].y = doscarte2[0].y + bmp1->h;

    Rect_NomJoueur[2].x = doscarte3[2].x;
    Rect_NomJoueur[2].y = doscarte3[3].y - Surf_NomJoueur[2]->h;

    Rect_NomJoueur[3].x = doscarte4[2].x;
    Rect_NomJoueur[3].y = doscarte4[3].y - Surf_NomJoueur[3]->h;

    SDL_BlitSurface(bmp, 0, screen, &dstrect);


    while(1)
    {
        SDL_Flip(screen);
        SDL_Event event;
        for(idx = 0;idx <3; idx++)
        {
            strcpy(&Nom_Carte[0][idx], "./Cartes/");

            switch(pSeg->Carte[idx].valeur)
            {
                case 1 : strcat(&Nom_Carte[0][idx], "As");
                         break;

                case 11 : strcat(&Nom_Carte[0][idx], "Valet");
                          break;

                case 12 : strcat(&Nom_Carte[0][idx], "Dame");
                          break;

                case 13 : strcat(&Nom_Carte[0][idx], "Roi");
                          break;

                default :
                          sprintf(Temp, "%d", pSeg->Carte[idx].valeur);
                          strcat(&Nom_Carte[0][idx], Temp);
                          break;
            }

            switch(pSeg->Carte[idx].signe)
            {
                case 1 : strcat(&Nom_Carte[0][idx], "_Coeur.bmp");
                         break;

                case 2 : strcat(&Nom_Carte[0][idx], "_Carreau.bmp");
                          break;

                case 3 : strcat(&Nom_Carte[0][idx], "_Pique.bmp");
                          break;

                case 4 : strcat(&Nom_Carte[0][idx], "_Trefle.bmp");
                          break;
            }
            if(idx == 0)
                bmp_Tab[0] = SDL_LoadBMP(&Nom_Carte[0][0]);
            if(idx == 1)
                bmp_Tab[1] = SDL_LoadBMP(&Nom_Carte[0][1]);
            if(idx == 2)
                bmp_Tab[2] = SDL_LoadBMP(&Nom_Carte[0][2]);
        }

      SDL_BlitSurface(bmp, 0, screen, &dstrect);
      switch(pSeg->Nb_Cartes_Restantes[0])
    {
        case 0:
            break;

        case 1:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[0]);
            break;

        case 2:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[0]);
            break;

        case 3:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[0]);
            break;

        default:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[3]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte1[0]);
            break;

    }
    switch(pSeg->Nb_Cartes_Restantes[1])
    {
        case 0:
            break;

        case 1:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[0]);
            break;

        case 2:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[0]);
            break;

        case 3:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[0]);
            break;

        default:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[3]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte2[0]);
            break;

    }
    switch(pSeg->Nb_Cartes_Restantes[2])
    {
        case 0:
            break;

        case 1:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[0]);
            break;

        case 2:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[0]);
            break;

        case 3:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[0]);
            break;

        default:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[3]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte3[0]);
            break;

    }
    switch(pSeg->Nb_Cartes_Restantes[3])
    {
        case 0:
            break;

        case 1:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[0]);
            break;

        case 2:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[0]);
            break;

        case 3:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[0]);
            break;

        default:
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[3]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[2]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[1]);
            SDL_BlitSurface(bmp1, 0, screen, &doscarte4[0]);
            break;

    }

        SDL_BlitSurface(tape1, 0, screen, &Push1);
        SDL_BlitSurface(tape1, 0, screen, &Push2);

        SDL_BlitSurface(TextTaper, NULL, screen, &RectTaper);
        SDL_BlitSurface(TextPoser, NULL, screen, &RectPoser);


        if(bmp_Tab[2] == NULL)
        {
            SDL_FreeSurface(bmp_Tab[2]);
        }
        else
        {
            SDL_BlitSurface(bmp_Tab[2], 0, screen, &Carte1);
        }
        if(bmp_Tab[1] == NULL)
        {
            SDL_FreeSurface(bmp_Tab[1]);
        }
        else
        {
            SDL_BlitSurface(bmp_Tab[1], 0, screen, &Carte2);
        }
        if(bmp_Tab[0] == NULL)
        {
            SDL_FreeSurface(bmp_Tab[0]);
        }
        else
        {
            SDL_BlitSurface(bmp_Tab[0], 0, screen, &Carte3);
        }


        SDL_BlitSurface(Surf_NomJoueur[0], 0, screen, &Rect_NomJoueur[0]);
        SDL_BlitSurface(Surf_NomJoueur[1], 0, screen, &Rect_NomJoueur[1]);
        SDL_BlitSurface(Surf_NomJoueur[2], 0, screen, &Rect_NomJoueur[2]);
        SDL_BlitSurface(Surf_NomJoueur[3], 0, screen, &Rect_NomJoueur[3]);
        switch(pSeg->Last_Joueur)
        {
            case 1:
                SDL_BlitSurface(Surf_Jeton, 0, screen, &RectJeton[0]);
                break;

            case 2:
                SDL_BlitSurface(Surf_Jeton, 0, screen, &RectJeton[1]);
                break;

            case 3:
                SDL_BlitSurface(Surf_Jeton, 0, screen, &RectJeton[2]);
                break;

            case 4:
                SDL_BlitSurface(Surf_Jeton, 0, screen, &RectJeton[3]);
                break;

        }
        SDL_BlitSurface(Surf_BoutonQuitter, NULL, screen, &Rect_Quitter);
        SDL_BlitSurface(Surf_TextQuitter, NULL, screen, &Rect_TextQuitter);
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                   cle = ftok("fic", '1');
                       if(cle == -1)
                       {
                           printf("ERREUR !!!!!!!!!!! \n");
                       }

                       shmid = shmget(cle, 0,0666);
                       perror("ERREUR : ");
                       printf("shmid : %d\n",shmid);
                       pSeg->Quitter = 1;
                       sleep(5);
                       if(shmdt(pSeg)==-1)
                           printf("ERREUR de Shmdt\n");
                       if(shmctl(shmid, IPC_RMID, NULL)==-1)
                            printf("ERREUR de Shmdctl\n");
                       exit(0);
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {

                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                    {
                       cle = ftok("fic", '1');
                       if(cle == -1)
                       {
                           printf("ERREUR !!!!!!!!!!! \n");
                       }
                       pSeg->Quitter = 1;
                       sleep(2);
                       shmid = shmget(cle, 0, IPC_CREAT|0666);
                       printf("TAille : sizeof(ST_IHM) = %ld\n",sizeof(ST_IHM));
                       printf("%d\n",errno);
                       printf("shmid : %d\n",shmid);


                       if(shmdt(pSeg)==-1)
                           printf("ERREUR de Shmdt\n");
                       if(shmctl(shmid, IPC_RMID, NULL)==-1)
                            printf("ERREUR de Shmdctl\n");
                       exit(0);
                    }

                    break;
                }
            case SDL_MOUSEBUTTONUP:
                if ((event.button.y > Push1.y) && (event.button.y <= (Push1.y +tape1->h )) && (event.button.x > Push1.x) && ((event.button.x <= (Push1.x + tape1->w))))// si clic sur boutton
                {
                    pSeg->Tapage = 0;
                }
                if ((event.button.y > Push2.y) && (event.button.y <= (Push2.y +tape1->h )) && (event.button.x > Push2.x) && ((event.button.x <= (Push2.x + tape1->w))))// si clic sur boutton
                {
                    // POSER
                    pSeg->Status_Joueur = 0;
                }
                if ((event.button.y > Rect_Quitter.y) && (event.button.y <= (Rect_Quitter.y +Surf_BoutonQuitter->h )) && (event.button.x > Rect_Quitter.x) && ((event.button.x <= (Rect_Quitter.x + Surf_BoutonQuitter->w))))// si clic sur boutton
                {
                    // POSER
                     cle = ftok("fic", '1');
                       if(cle == -1)
                       {
                           printf("ERREUR !!!!!!!!!!! \n");
                       }
                       pSeg->Quitter = 1;
                       sleep(2);
                       shmid = shmget(cle, 0, IPC_CREAT|0666);
                       printf("TAille : sizeof(ST_IHM) = %ld\n",sizeof(ST_IHM));
                       printf("%d\n",errno);
                       printf("shmid : %d\n",shmid);


                       if(shmdt(pSeg)==-1)
                           printf("ERREUR de Shmdt\n");
                       if(shmctl(shmid, IPC_RMID, NULL)==-1)
                            printf("ERREUR de Shmdctl\n");
                       exit(0);
                }
                break;

            default:
                if(pSeg->Status_Partie >0)
                {
                 cle = ftok("fic", '1');
                       if(cle == -1)
                       {
                           printf("ERREUR !!!!!!!!!!! \n");
                       }
                       pSeg->Quitter = 1;
                       sleep(2);
                       shmid = shmget(cle, 0, IPC_CREAT|0666);
                       printf("TAille : sizeof(ST_IHM) = %ld\n",sizeof(ST_IHM));
                       printf("%d\n",errno);
                       printf("shmid : %d\n",shmid);


                       if(shmdt(pSeg)==-1)
                           printf("ERREUR de Shmdt\n");
                       if(shmctl(shmid, IPC_RMID, NULL)==-1)
                            printf("ERREUR de Shmdctl\n");
                       exit(0);

                }
                break;
            } // end switch
        } // end of message processing
    }

}

void AfficheRegles(void)
{
    FILE* fichier = NULL;
    char regles[TAILLE_MAX] = "";
    int idx = 0;

    fichier = fopen("Regles_Jeu", "r");

    if (fichier != NULL)
    {
        SDL_FreeSurface(Bouton1);
        SDL_FreeSurface(Image_fond);
        SDL_FreeSurface(TextSurface);
        SDL_FreeSurface(TextBouton1);
        SDL_FreeSurface(TextBouton2);
        SDL_FreeSurface(TextBouton3);
        SDL_FreeSurface(TextBouton4);
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
        TextRegles.x = 0;
        TextRegles.y = 0;
        TextRegles.w = screen->w;
        TextRegles.h = screen->h;

        while(fgets(regles, 120, fichier) != NULL)
        {
            while(regles[idx] != '\n' && regles[idx] != '\0')
            {
                idx ++;
            }

            regles[idx] = 0;
            SDL_Flip(screen);
            Affiche_FichierRegles = TTF_RenderText_Blended(Police_Regles, regles, TextBlanc);
            SDL_BlitSurface(Affiche_FichierRegles, NULL, screen, &TextRegles);
            memset(regles, 0, TAILLE_MAX);
            TextRegles.y = TextRegles.y  + 15;
            idx = 0;
        }

        fclose(fichier);
        Rect_Quitter.x= (screen->w/2) - (Surf_BoutonQuitter->w/2) ;
        Rect_Quitter.y= (screen->h*5/6);

        Rect_TextQuitter.x= (screen->w/2) - (Surf_TextQuitter->w/2) ;
        Rect_TextQuitter.y= (screen->h*5/6) + (Surf_TextQuitter->h/2);

        SDL_BlitSurface(Surf_BoutonQuitter, NULL, screen, &Rect_Quitter);
        SDL_BlitSurface(Surf_TextQuitter, NULL, screen, &Rect_TextQuitter);
        SDL_Flip(screen);
        // message processing loop
        SDL_Event event;
    while(1)
    {

        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                       cle = ftok("fic", '1');
                       if(cle == -1)
                       {
                           printf("ERREUR !!!!!!!!!!! \n");
                       }

                       shmid = shmget(cle, 0,0666);
                       perror("ERREUR : ");
                       printf("shmid : %d\n",shmid);
                       pSeg->Quitter = 1;
                       sleep(5);
                       if(shmdt(pSeg)==-1)
                           printf("ERREUR de Shmdt\n");
                       if(shmctl(shmid, IPC_RMID, NULL)==-1)
                            printf("ERREUR de Shmdctl\n");
                       exit(0);
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                           cle = ftok("fic", '1');
                           if(cle == -1)
                           {
                               printf("ERREUR !!!!!!!!!!! \n");
                           }

                           shmid = shmget(cle, 0,0666);
                           perror("ERREUR : ");
                           printf("shmid : %d\n",shmid);
                           pSeg->Quitter = 1;
                           sleep(5);
                           if(shmdt(pSeg)==-1)
                               printf("ERREUR de Shmdt\n");
                           if(shmctl(shmid, IPC_RMID, NULL)==-1)
                                printf("ERREUR de Shmdctl\n");
                           exit(0);
                    break;
                }
            case SDL_MOUSEBUTTONUP:

                if((event.button.y > Rect_Quitter.y) && (event.button.y <= (Rect_Quitter.y +Surf_BoutonQuitter->h )) && (event.button.x > Rect_Quitter.x) && ((event.button.x <= (Rect_Quitter.x + Surf_BoutonQuitter->w))))
                {
                    printf("VOUS AVEZ CLICER DANS LA CASE\n");
                    return;
                }

            default:
                break;
            } // end switch
        } // end of message processing
    }

    }
}
